<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/28
 * Time: 14:37
 */

namespace Home\Controller;

use Admin\Model\ContactUs;
use Admin\Model\Introduction;
use Admin\Model\News;
use Admin\Model\NewsCategories;
use Admin\Model\Product;
use Admin\Model\ProductCategories;
use Common\Common\MarkdownParser;
use Common\Common\Validator;
use Think\Controller;

class WebController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->assign('contact', ContactUs::findOne());
    }


    public function index() {
        $about = Introduction::findByTitle('公司简介');
        if (!empty($about)) $about['content'] = MarkdownParser::parserHtml($about['content']);
        $products = Product::getInstance()->order('updated_at desc')->limit(0, 6)->select();
        $this->assign('about', $about);
        $this->assign('products', $products);

        $data = $this->banner();
        $model = M('news');
        $info = $model->select();

        $infos = $model->limit(5)->select();
        $this->assign('infos',$infos);
        $this->assign('info',$info);
        $this->assign('data',$data);
        $this->display('index');
    }

    public function product() {
        $categories = ProductCategories::all();
        $category_id = I('get.id', 0);
        if (intval($category_id) == 0 && !empty($categories)) {
            $category_id = $categories[0]['id'];
        }

        $page = I('get.page', 1);
        if (intval($page) == 0) {
            $page = 1;
        }
        if ($category_id === null) {
            $category_id = 0;
        }
        $products = Product::findByCategoryIdToPagination($page, $category_id, 12);
        $this->assign('categories', $categories);
        $this->assign('category_id', $category_id);
        $this->assign('products', $products);
        $this->assign('data', $products->data);
        $this->display('product');
    }
    public function news(){
        $info = M('new_categories');
        $model = M('news');
        $data = $model->select();
        $infos = $info->select();

        $this->assign('infos',$infos);
        $this->assign('data',$data);
        $this->display('news');

//        $categories = NewsCategories::all();
//        $category_id = I('get.id', 0);
//        if (intval($category_id) == 0 && !empty($categories)) {
//            $category_id = $categories[0]['id'];
//        }
//
//        $page = I('get.page', 1);
//        if (intval($page) == 0) {
//            $page = 1;
//        }
//        if ($category_id === null) {
//            $category_id = 0;
//        }
//        $news = News::findByCategoryIdToPagination($page, $category_id, 12);
//        $this->assign('categories', $categories);
//        $this->assign('category_id', $category_id);
//        $this->assign('news', $news);
//        $this->assign('data', $news->data);

    }
    public function news_detail()
    {
        $id = $_GET['id'];
        $info = M('new_categories');
        $model = M('news');
        $data = $model->where("id = $id")->select();
        $infos = $info->select();

        //解析编辑器的内容
        foreach ($data as $key => $item){
            $data[$key]['news_content'] = MarkdownParser::parserHtml($item['news_content']);
        }
        $this->assign('infos',$infos);
        $this->assign('data',$data);
        $this->display('news_detail');
    }


    public function banner(){
        $model = M('banner');
        $data = $model->select();
        return $data;
    }

    public function about() {
        $types = Introduction::getTypes();
        $v = Validator::make($_REQUEST, ['type' => 'required|exists:introduction,type']);
        if ($v->filed()) {
            $type = empty($types) ? '' : $types[0]['type'];
        } else {
            $type = $_REQUEST['type'];
        }
        $data = Introduction::getByTypeName($type);
        foreach ($data as $key => $item) {
            $parser = new MarkdownParser();
            $data[$key]['content'] = $parser->makeHtml($item['content']);
        }
        $this->assign('types', $types);
        $this->assign('data', $data);
        $this->assign('type', $type);
        $this->display('about');
    }

    public function contact() {
        $this->display('contact');
    }

    public function pro_details()
    {
        $validator = Validator::make($_REQUEST, [
           'id' => ['required', 'exists:products,id']
        ]);
        if ($validator->filed()) {
            $this->error('数据不存在');
        }

        $product = Product::findById($_REQUEST['id']);
        $product['content'] = MarkdownParser::parserHtml($product['content']);
        $categories = ProductCategories::all();
        $this->assign('product', $product);
        $this->assign('categories', $categories);
        $this->display('pro_details');
    }



}